import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_demo/api/news_api.dart';
import 'package:flutter_demo/entity/base_response.dart';
import 'package:flutter_demo/entity/news_item.dart';
import 'package:flutter_demo/page/base_page_state.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class NewsPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return NewsPageState();
  }
}

class NewsPageState extends BasePageState<NewsPage> {
  RefreshController _refreshController = RefreshController(initialRefresh: false);
  NewsApi _newsApi = NewsApi();
  List<NewsItem> newsList = List();

  @override
  void initState() {
    super.initState();
    appBarController.title = '新闻';
    addAppBarTextAction('test', () {
      print('click');
    });
    addAppBarImageAction('res/images/ic_share.png', () {
      showMenu(
          context: context,
          position: RelativeRect.fill,
          items: <PopupMenuEntry>[
            PopupMenuItem(
              value: 'test',
              child: Text('Home'),
            )
          ]);

    });
    _fetchData();
  }

  @override
  Widget buildBody(BuildContext context) {
    return Container(
        padding: EdgeInsets.all(10),
        child: SmartRefresher(
          header: WaterDropHeader(),
          child: ListView.separated(
            itemBuilder: _buildItem,
            physics: BouncingScrollPhysics(),
            separatorBuilder: (BuildContext context, int index) {
              return Container(
                height: 10,
              );
            },
            itemCount: newsList.length,
          ),
          controller: _refreshController,
          onRefresh: _onRefresh,
        )
    );
  }

  Widget _buildItem(BuildContext context, int index) {
    NewsItem item = newsList[index];
    return Card(
        child: Container(
            padding: EdgeInsets.all(10),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text(
                  '${item.name}',
                  style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
                ),
                Text(
                  '创建日期：${item.create_time}',
                  style: TextStyle(fontSize: 12, color: Colors.black54),
                ),
                AspectRatio(
                  aspectRatio: 2,
                  child: CachedNetworkImage(
                    imageUrl: item.img,
                  )
                ),
                Text.rich(
                  TextSpan(
                    children: [
                      TextSpan(
                        text: '热点',
                        style: TextStyle(fontSize: 14, color: Colors.white, backgroundColor: Colors.red)
                      ),
                      TextSpan(
                        text: '${item.summary}',
                        style: TextStyle(fontSize: 14, color: Colors.black87),
                      )
                    ]
                  )
                ),
                Text(
                  '${item.tip}',
                  style: TextStyle(fontSize: 14),
                )
              ],
            )
        )
    );
  }

  _fetchData() async {
    BaseResponse response = await _newsApi.getNewsList();
    List<NewsItem> list = List();
    var content = response.data['content'];
    for (var item in content) {
      NewsItem newsItem = NewsItem.fromJson(item);
      list.add(newsItem);
    }
    setState(() {
      newsList = list;
    });
  }

  _onRefresh() async {
    await _fetchData();
    _refreshController.refreshCompleted();
  }
}
